import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'

const WEBNAME="CodeBlog";
const router = createRouter({
    linkActiveClass: 'active',
    history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
    routes: [
        {
            path: '/',
            name: '/',
            component: () => import('@/views/custom/Home.vue'),
            meta: {
                title: '首页'
            }
        },
        {
            path: '/home',
            name: 'home',
            component: () => import('@/views/custom/Home.vue'),
            meta: {
                title: '首页'
            }
        },
        {
            path: '/blog',
            name: 'blog',
            component: () => import('@/views/custom/Blog.vue'),
            meta: {
                title: '文章'
            }
        },
        {
            path: '/category',
            name: 'category',
            component: () => import('@/views/custom/Category.vue'),
            meta: {
                title: '分类'
            }
        },
        {
            path: '/tools',
            name: 'tools',
            component: () => import('@/views/custom/Tools.vue'),
            meta: {
                title: '工具'
            }
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/custom/Login.vue'),
            meta: {
                title: '登录'
            }
        },
        {
            path: '/signin',
            name: 'signin',
            component: () => import('@/views/custom/SignIn.vue'),
            meta: {
                title: '注册'
            }
        },
        {
            path: '/edit/:id',
            name: 'edit',
            component: () => import('@/views/custom/Editor.vue'),
            meta: {
                title: '编辑文章'
            }
        },
        {
            path: '/articles/:id',
            name: 'articles',
            component: () => import('@/views/custom/Articles.vue'),
            meta: {
                title: '文章'
            }
        },
        {
            path: '/userCenter',
            name: 'userCenter',
            component: () => import('@/views/custom/UserCenter.vue'),
            meta: {
                title: '用户中心'
            },
            children:[
                {
                    path: '/userCenter/userInfo',
                    name: 'userInfo',
                    component: () => import('@/views/custom/user/UserInfo.vue'),
                    meta: {
                        title: '用户信息'
                    },
                },
                {
                    path: '/userCenter/editUser',
                    name: 'editUser',
                    component: () => import('@/views/custom/user/EditUser.vue'),
                    meta: {
                        title: '修改用户信息'
                    },
                },
                {
                    path: '/userCenter/manageBlog',
                    name: 'manageBlog',
                    component: () => import('@/views/custom/user/ManageBlog.vue'),
                    meta: {
                        title: '管理文章'
                    },
                },
                {
                    path: '/userCenter/manageTag',
                    name: 'manageTag',
                    component: () => import('@/views/custom/user/ManageTag.vue'),
                    meta: {
                        title: '管理专栏'
                    },
                },
                {
                    path: '/userCenter/manageGroup',
                    name: 'manageGroup',
                    component: () => import('@/views/custom/user/ManageGroup.vue'),
                    meta: {
                        title: '管理分组'
                    },
                },
                {
                    path: '/userCenter/favourBlog',
                    name: 'favourBlog',
                    component: () => import('@/views/custom/user/FavourBlog.vue'),
                    meta: {
                        title: '喜欢的文章'
                    },
                },
            ]

        },
        {
            path: '/about',
            name: 'about',
            component: () => import('@/views/custom/About.vue'),
            meta: {
                title: '关于'
            }
        }
    ]
})

router.beforeEach((to, from, next)=>{
    if (to.name==='edit'){
        store.dispatch("setEdit",true);
    }else store.dispatch("setEdit",false);

    if(to.meta.title){
        document.title = WEBNAME+"👀"+to.meta.title
    }
    if (to.path !== from.path) {
        store.dispatch('setLoading', true);
    }
    next()
})

// eslint-disable-next-line no-unused-vars
router.afterEach((to, from) => {
    // 最多延迟 关闭 loading
    setTimeout(() => {
        store.dispatch('setLoading', false);
    }, 500)
})

export default router
